package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseAssistDataMapper;
import com.tsd.base.entity.BaseAssistData;
import com.tsd.base.entity.BaseAssistDataExt;
import com.tsd.base.service.BaseAssistDataService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseAssistData 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseAssistDataServiceImpl extends BaseServiceImpl implements BaseAssistDataService {

    @Resource
    private BaseAssistDataMapper baseAssistDataMapper;

    @Override
    public BaseAssistData getBaseAssistDataById(Long id) throws Exception {
        return baseAssistDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseAssistDataExt queryBySid(String sid) throws Exception {
        return baseAssistDataMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseAssistData(String sid, SysUser opUser) throws Exception {
        BaseAssistData old = baseAssistDataMapper.selectBySid(sid);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseAssistDataMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public BaseAssistData saveBaseAssistData(BaseAssistData record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getAssist_type_sid(), "分类的sid不能为空！");
            super.checkEmpty(record.getName(), "名称不能为空！");
            super.checkEmpty(record.getCode(), "编码不能为空！");
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseAssistDataMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseAssistDataMapper.updateByPrimaryKeySelective(record);
        }
        // 校验分类下编码不能重复
        if (!HlpUtils.isEmpty(record.getAssist_type_sid()) && !HlpUtils.isEmpty(record.getCode())) {
            int num = baseAssistDataMapper.countTypeCode(record.getAssist_type_sid(), record.getCode());
            if (num > 1) {
                throw new HlpException("编号重复，请重新输入！");
            }
        }
        return record;
    }

    @Override
    public List<BaseAssistDataExt> findBaseAssistDatas(Map<String, Object> params, PageBean page) throws Exception {
        return baseAssistDataMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseAssistDataExt> findBaseAssistDatas(Map<String, Object> params) throws Exception {
        return baseAssistDataMapper.selectByParams(params);
    }

    @Override
    public void batchDeleteBaseAssistData(List<String> sidList, SysUser opUser) throws Exception {
        for (String sid : sidList) {
            deleteBaseAssistData(sid, opUser);
        }
    }

    @Override
    public Map<String, Object> findBaseAssistDataFromTypeNameList(List<String> codeList, SysUser opUser) {
        Map<String, Object> map = new HashMap<>(8);
        for (String code : codeList) {
            List<BaseAssistDataExt> dataExtList = baseAssistDataMapper.selectByCode(code);
            if (!HlpUtils.isEmptyList(dataExtList)) {
                map.put(dataExtList.get(0).getType_code(), dataExtList);
            }
        }
        return map;
    }
}
